package i2.application.checklist.struts.actions;

import i2.application.checklist.entities.Categorie;
import i2.application.checklist.entities.CheckList;
import i2.application.checklist.entities.Question;
import i2.application.checklist.entities.Rapport;
import i2.application.checklist.entities.Reponse;
import i2.application.checklist.services.CheckListService;
import i2.application.checklist.struts.utils.Questionnaire;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;


public class CheckListAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(CheckListAction.class);

	private CheckListService checkListService;

	private Map<String, Object> mySession;
	
	/**
	 * <b>LECTURE/ECRITURE</b>
	 */
	private List<Question> questionList;


	private List<Reponse> reponseList;
	/**
	 * <b>LECTURE</b>
	 */
	private Categorie categorie;

	private List<List<Reponse>> reponseGlobalList;
	
	/**
	 * id de la CheckList
	 * <b>ECRITURE</b>
	 */
	private int id;
	
	private CheckList checkList;
	
	private Rapport rapport;
	
	public String consulter() {
		if (id == 0) {
			logger.error("l'identifiant n'existe pas");
			return ERROR;
		}
		
		checkList = checkListService.getCheckList(new CheckList(id));
		
		if (checkList == null) {
			logger.error("{} n'est pas un identifiant de checklist connu", id);
			return ERROR;
		}
		
		return "view";
	}
	
	public String init() {
		if (id == 0) {
			logger.error("cette méthode nécessite un identifiant");
			return ERROR;
		}
		
		CheckList checkList = checkListService.getCheckList(new CheckList(id)); 

		if (checkList == null) {
			logger.error("{} n'est pas un identifiant de checkList connu", id);
			return ERROR;
		}
		
		Questionnaire questionnaire = new Questionnaire();
		questionnaire.init(checkList);
		mySession.put("questionnaire", questionnaire);
		
		return "init";
	}

	public String creer() {

		Questionnaire questionnaire = (Questionnaire) mySession.get("questionnaire");
		
		categorie = questionnaire.nextCategorie();
		
		if (categorie == null) {
			logger.debug("no more Categorie, moving to resume page");
			return "resume";
		}
		
		logger.debug("categorie : {}", categorie);

		questionList = checkListService.getQuestionListByCategorie(categorie);
		logger.debug("questionList : {}", questionList);

		return "creer";
	}
	
	public String repondre() {

		Questionnaire questionnaire = (Questionnaire) mySession.get("questionnaire");
		
		reponseList = checkListService.remplirReponses(reponseList);
		
		logger.debug("adding reponseList : {}", reponseList);
		
		questionnaire.addReponses(reponseList);
		
		return "repondre";
	}
	
	public String resume() {
		Questionnaire questionnaire = (Questionnaire) mySession.get("questionnaire");
		
		reponseGlobalList = questionnaire.getReponseList();
		
		return "resumeok";
	}
	
	
	
	public String precedent() {

		Questionnaire questionnaire = (Questionnaire) mySession.get("questionnaire");
		
		Categorie categorie = questionnaire.goToPreviousQuestions();
		logger.debug("categorie : {}", categorie);
		return "repondre";
	}
	
	public String valider() {
		Questionnaire questionnaire = (Questionnaire) mySession.get("questionnaire");
		
		logger.debug("reponseGlobalList : {}", reponseGlobalList);

		checkList = questionnaire.getCheckList();
		checkList.setDone(true);
		checkList = checkListService.updateCheckList(checkList);
		
		if (!checkList.getRapport().isOk()) {
			rapport  = checkList.getRapport();
			logger.debug("checkList id : {}", checkList);
			id = checkList.getId();
			return "rapport";
		}
		
		return SUCCESS;
	}

	public void setCheckListService(CheckListService checkListService) {
		this.checkListService = checkListService;
	}

	public Categorie getCategorie() {
		return categorie;
	}

	public List<Question> getQuestionList() {
		return questionList;
	}

	public void setQuestionList(List<Question> questionList) {
		this.questionList = questionList;
	}

	public List<List<Reponse>> getReponseGlobalList() {
		return reponseGlobalList;
	}

	public void setReponseGlobalList(List<List<Reponse>> reponseGlobalList) {
		this.reponseGlobalList = reponseGlobalList;
	}

	public List<Reponse> getReponseList() {
		return reponseList;
	}

	public void setReponseList(List<Reponse> reponseList) {
		this.reponseList = reponseList;
	}

	public void setId(int id) {
		this.id = id;
	}

	public CheckList getCheckList() {
		return checkList;
	}
	
	public Rapport getRapport() {
		return rapport;
	}

	public void setRapport(Rapport rapport) {
		this.rapport = rapport;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		mySession = session;
	}
	
}
